package com.thuan.hibernate;

import java.util.Date;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Competition;
import pojo.League;
import pojo.Privilege;
import pojo.Reward;
import pojo.UserAccount;
import com.thuan.db.LeagueDAO;
import com.thuan.db.UserDAO;

public class Repository {
	private static ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
	private static UserDAO userBo = (UserDAO)context.getBean("userDAO");
	private static LeagueDAO leagueBo = (LeagueDAO) context.getBean("leagueDAO");	
	
	public static List<UserAccount> getUsers() {
		return userBo.getUsers();
	}
	
	public static int addUser(String username, String password, String name, int level) {
		return userBo.addUser(username, password, name, level);
	}
	
	public static UserAccount findUser(String username) {
		return userBo.findUser(username);
	}
	
	public static boolean updateUserAccount(String username, String password, String name,
			int score, int win, int lose, Privilege privilege) {
		return userBo.updateUserAccount(username, password, name, score, win, lose, privilege);
	}
	
	public static boolean addUserAccount(String username, String password, String name,
			int score, int win, int lose, Privilege privilege) {
		return userBo.addUserAccount(username, password, name, score, win, lose, privilege);
	}
	
	public static boolean updatePassword(String username, String password) {
		return userBo.updatePassword(username, password);
	}
	
	public static void updateSigninTime(String username) {
		userBo.updateSigninTime(username);
	}
	
	public static List<League> listLeagues() {
		return leagueBo.listLeagues();
	}
	
	public static List<Reward> listRewards() {
		return leagueBo.listRewards();
	}
	
	public static int addLeague(String name, Date startDate, Date endDate,
			int rewardId) {
		return leagueBo.addLeague(name, startDate, endDate, rewardId);
	}
	
	public static Competition findCompetition(UserAccount playerA, UserAccount playerB) {
		return leagueBo.findCompetition(playerA, playerB);
	}
	
	public static int addCompetition(int leagueId, String usernamePlayerA,
			String usernamePlayerB, String usernameWinner) {
		return leagueBo.addCompetition(leagueId, usernamePlayerA, usernamePlayerB, usernameWinner);
	}
	
	public static List<UserAccount> getWinners(League league) {
		return leagueBo.getWinners(league);
	}
	
	public static League findLeague(String name) {
		return leagueBo.findLeague(name);
	}

	public static League findLeague(int leagueId) {
		return leagueBo.findLeague(leagueId);
	}
	
	public static boolean login(String username, String password) {
		return userBo.login(username, password);
	}
	public static void updateScore(String username,int score,int win,int lose){
		userBo.updateScore(username, score, win, lose);
	}
	
	public static int addLeagueMember(int leagueId, String usernamePlayerId, int leagueScore) {
		return leagueBo.addLeagueMember(leagueId, usernamePlayerId, leagueScore);
	}

	public static void updateLeagueScore(int leagueId, String usernamePlayerId, int leagueRoomScore) {
		leagueBo.updateLeagueScore(leagueId, usernamePlayerId, leagueRoomScore);
	}

	public static int countMemberOfLeague(League league){
		return leagueBo.countMemberOfLeague(league);
	}
	
	public static List<UserAccount> listPlayersOfScore(int leagueId, int leagueScore){
		return leagueBo.listPlayersOfScore(leagueId, leagueScore);
	}
	
	public static void updateRewardForWinner(String username, int prize){
		leagueBo.updateRewardForWinner(username, prize);
	}
	
	public static List<Integer> topLeagueScore(int leagueId){
		return leagueBo.topLeagueScore(leagueId);
	}
	
	public static Reward findReward(int rewardId){
		return leagueBo.findReward(rewardId);
	}
	
	public static void updateLeague(int leagueId, String name, Reward reward, Date startDate, Date endDate){
		leagueBo.updateLeague(leagueId, name, reward, startDate, endDate);
	}
	
	public static boolean deleteLeague(int leagueId){
		return leagueBo.deleteLeague(leagueId);
	}
	
	public static List<UserAccount> listUserAccounts(){
		return userBo.listUserAccounts();
	}
	
	public static List<Privilege> listPrivileges(){
		return userBo.listPrivileges();
	}
	
	public static boolean deleteUserAccount(String username){
		return userBo.deleteUserAccount(username);
	}
}
